package org.laobai.auth.controller;

import org.laobai.auth.service.AuthService;
import org.laobai.common.struct.Pair;
import org.laobai.common.struct.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;

@RestController
@RequestMapping("auth")
public class AuthController {
    @Autowired
    private AuthService authService;

    @GetMapping("login")
    public Result login(String name , String pwd, HttpServletResponse response){
        Pair<Boolean, String> pair = authService.login(name, pwd);
        if(pair.getKey()){
            response.addCookie(new Cookie("Authorize",pair.getValue()));
            return new Result(true,200,"登录成功",pair.getValue());
        }
        return new Result(true, 500,"登录失败");

    }

}
